/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * searchBook.java
 *
 * Created on Sep 16, 2010, 1:10:44 PM
 */

package COMIC_BOOK_STORE;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author DeKNOR
 */
public class BOOK_SEARCH extends javax.swing.JFrame {

    Connection con;
    Statement stat;
    ResultSet rset;
    String Bcode;
    String Mcode;
   DefaultListModel List;

    /** Creates new form searchBook */
    public BOOK_SEARCH() throws SQLException {
        super("SEARCH BOOK");
        initComponents();
        this.setLocationRelativeTo(null);
        connect();
        populateData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        backButton = new javax.swing.JButton();
        findButton = new javax.swing.JButton();
        genreComboBox = new javax.swing.JComboBox();
        genreLabel = new javax.swing.JLabel();
        searchBookLabel = new javax.swing.JLabel();
        RentButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        bookCodeLabel = new javax.swing.JLabel();
        bookTitleLabel = new javax.swing.JLabel();
        bookauthorLabel = new javax.swing.JLabel();
        bookPublisherLabel = new javax.swing.JLabel();
        bookavailableLabel = new javax.swing.JLabel();
        bookISBNLabel = new javax.swing.JLabel();
        bookCodeTextField = new javax.swing.JTextField();
        displayBookTitle = new javax.swing.JTextField();
        displayBookAuthor = new javax.swing.JTextField();
        displayBookPublisher = new javax.swing.JTextField();
        displayIsbn = new javax.swing.JTextField();
        displayBookCode = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        bookList = new javax.swing.JList();
        availableComboBox = new javax.swing.JComboBox();
        bookCodeLabel2 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        memberCodeLabel = new javax.swing.JLabel();
        displaymemberCode = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        backButton.setText("BACK");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });
        backButton.setBounds(720, 450, 110, 40);
        jLayeredPane1.add(backButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        findButton.setText("FIND");
        findButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findButtonActionPerformed(evt);
            }
        });
        findButton.setBounds(550, 80, 110, 30);
        jLayeredPane1.add(findButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        genreComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "SELECT", "COMEDY", "ANIME", "ADVENTURE", "ROMANTIC", "CRIME FICTION", "SUPERHERO" }));
        genreComboBox.setBounds(230, 80, 240, 30);
        jLayeredPane1.add(genreComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        genreLabel.setText("Genre");
        genreLabel.setBounds(70, 80, 70, 30);
        jLayeredPane1.add(genreLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        searchBookLabel.setText("SEARCH BOOK");
        searchBookLabel.setBounds(40, 30, 120, 14);
        jLayeredPane1.add(searchBookLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        RentButton.setText("RENT");
        RentButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RentButtonActionPerformed(evt);
            }
        });
        RentButton.setBounds(600, 450, 110, 40);
        jLayeredPane1.add(RentButton, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator1.setBounds(40, 52, 400, 10);
        jLayeredPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookCodeLabel.setText("Book Code");
        bookCodeLabel.setBounds(70, 130, 90, 30);
        jLayeredPane1.add(bookCodeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookTitleLabel.setText("Book Title");
        bookTitleLabel.setBounds(450, 240, 120, 30);
        jLayeredPane1.add(bookTitleLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookauthorLabel.setText("Book Author");
        bookauthorLabel.setBounds(450, 280, 120, 30);
        jLayeredPane1.add(bookauthorLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookPublisherLabel.setText("Book Publisher");
        bookPublisherLabel.setBounds(450, 320, 130, 30);
        jLayeredPane1.add(bookPublisherLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookavailableLabel.setText("Availability");
        bookavailableLabel.setBounds(450, 400, 120, 30);
        jLayeredPane1.add(bookavailableLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookISBNLabel.setText("Book ISBN");
        bookISBNLabel.setBounds(450, 360, 120, 30);
        jLayeredPane1.add(bookISBNLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        bookCodeTextField.setBounds(230, 130, 130, 30);
        jLayeredPane1.add(bookCodeTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);
        displayBookTitle.setBounds(600, 240, 190, 30);
        jLayeredPane1.add(displayBookTitle, javax.swing.JLayeredPane.DEFAULT_LAYER);
        displayBookAuthor.setBounds(600, 280, 190, 30);
        jLayeredPane1.add(displayBookAuthor, javax.swing.JLayeredPane.DEFAULT_LAYER);
        displayBookPublisher.setBounds(600, 320, 190, 30);
        jLayeredPane1.add(displayBookPublisher, javax.swing.JLayeredPane.DEFAULT_LAYER);
        displayIsbn.setBounds(600, 360, 190, 30);
        jLayeredPane1.add(displayIsbn, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayBookCode.setFocusable(false);
        displayBookCode.setBounds(600, 200, 190, 30);
        jLayeredPane1.add(displayBookCode, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        bookList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                bookListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(bookList);

        jScrollPane1.setBounds(70, 190, 250, 230);
        jLayeredPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        availableComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "YES ", "NO" }));
        availableComboBox.setBounds(600, 400, 90, 30);
        jLayeredPane1.add(availableComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookCodeLabel2.setText("Book Code");
        bookCodeLabel2.setBounds(450, 200, 120, 30);
        jLayeredPane1.add(bookCodeLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel7.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel7.setBounds(410, 190, 420, 250);
        jLayeredPane1.add(jLabel7, javax.swing.JLayeredPane.DEFAULT_LAYER);

        memberCodeLabel.setText("Please enter your member code ==>");
        memberCodeLabel.setBounds(410, 130, 260, 30);
        jLayeredPane1.add(memberCodeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        displaymemberCode.setBounds(690, 130, 140, 30);
        jLayeredPane1.add(displaymemberCode, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGE_REPOSITORY/Background5 copy.jpg"))); // NOI18N
        jLabel1.setBounds(-40, 0, 940, 520);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 901, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
     new MAIN_MENU().setVisible(true);
        this.setVisible(false);

    }                                          

    public static String Charge1;
    public static String Charge2;
  // public static String totalCharge;

    private void findButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        String query = null ;
        String choice = bookCodeTextField.getText();
        String combo_select = (String) genreComboBox.getSelectedItem();
        String avai;
        DefaultListModel TempList;

       if (combo_select.equals("SELECT") && choice.equals(""))
        {
            JOptionPane.showMessageDialog(null,"Please Select/Fill first!" );
        }
        else
        {
            if(choice.equals(""))
            {
                query = "SELECT * FROM BOOK WHERE BOOK_GENRE = '"+combo_select+"'";
                try {

                    TempList = new DefaultListModel();
                    stat = con.createStatement();
                    rset = stat.executeQuery(query);

                    while(rset.next())
                    {
                        TempList.addElement(rset.getString("BOOK_TITLE"));
                    }
                    bookList.setModel(TempList);
                  
                    }
                    catch (SQLException ex)
                    {
                        Logger.getLogger(BOOK_SEARCH.class.getName()).log(Level.SEVERE, null, ex);
                    }
           }
           else if(combo_select.equals("SELECT"))
           {
               query = "SELECT * FROM BOOK WHERE BOOK_CODE='"+choice+"'";
                try {

                    stat = con.createStatement();
                    rset = stat.executeQuery(query);

                     if(rset.next()){

                        displayBookCode.setText(rset.getString("BOOK_CODE"));
                        displayBookTitle.setText(rset.getString("BOOK_TITLE"));
                        displayBookAuthor.setText(rset.getString("BOOK_AUTHOR"));
                        displayBookPublisher.setText(rset.getString("BOOK_PUBLISHER"));
                      // availableComboBox.setText(rset.getString("BOOK_AVAILABLE"));
                        displayIsbn.setText(rset.getString("BOOK_ISBN"));
                        Bcode = rset.getString("BOOK_CODE");
                        Mcode = rset.getString("MEMBER_CODE");
                        Charge1 = rset.getString("BOOK_CHARGE");
                        Charge1.trim();
                        Charge2 = rset.getString("BOOK_COST");
                        Charge2.trim();

                      // Double a= Double.parseDouble(Charge1) ;
                       //Double b= Double.parseDouble(Charge2);
                      // Double total= a+b;

                     //  totalCharge = total.toString();

                        //System.out.println("charge = "+totalCharge);
                        avai = rset.getString("BOOK_AVAILABLE");
                        if(avai.equals("YES"))
                        {
                            availableComboBox.setSelectedIndex(0);
                        }
                        else
                        {
                            availableComboBox.setSelectedIndex(1);
                        }
                    }
                }
                catch (SQLException ex)
                {
                    Logger.getLogger(BOOK_SEARCH.class.getName()).log(Level.SEVERE, null, ex);
                }
           }
           else
           {
                query = "SELECT * FROM BOOK WHERE BOOK_GENRE = '"+combo_select+"' AND BOOK_CODE='"+choice+"'";
                try {

                    stat = con.createStatement();
                    rset = stat.executeQuery(query);

                     if(rset.next()){

                        displayBookCode.setText(rset.getString("BOOK_CODE"));
                        displayBookTitle.setText(rset.getString("BOOK_TITLE"));
                        displayBookAuthor.setText(rset.getString("BOOK_AUTHOR"));
                        displayBookPublisher.setText(rset.getString("BOOK_PUBLISHER"));
                      // availableComboBox.setText(rset.getString("BOOK_AVAILABLE"));
                        displayIsbn.setText(rset.getString("BOOK_ISBN"));
                        Bcode = rset.getString("BOOK_CODE");
                        Mcode = rset.getString("MEMBER_CODE");
                        Charge1 = rset.getString("BOOK_CHARGE ");
                        Charge1.trim();
                        Charge2 = rset.getString("BOOK_COST ");
                        Charge2.trim();

                     //  Double a= Double.parseDouble(Charge1) ;
                      // Double b= Double.parseDouble(Charge2);
                       //Double total= a+b;

                      // totalCharge = total.toString();
                  //   System.out.println()




                        avai = rset.getString("BOOK_AVAILABLE");
                        if(avai.equals("YES"))
                        {
                            availableComboBox.setSelectedIndex(0);
                        }
                        else
                        {
                            availableComboBox.setSelectedIndex(1);
                        }
                    }
                }
                catch (SQLException ex)
                {
                    Logger.getLogger(BOOK_SEARCH.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        }

 // 
    }                                          

    private void RentButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

        String a = displayBookCode.getText();
        String b = displaymemberCode.getText();

        if(a.equals(""))
        {
            JOptionPane.showMessageDialog(null, "Please select/fill first!");
        }
        else if(b.equals(""))
            {
            JOptionPane.showMessageDialog(null, "Please fill member code first!");
        }
        else
        {
            try
            {
                new BOOK_RENT(Bcode,b).setVisible(true);
            }
            catch (SQLException ex)
            {
                Logger.getLogger(BOOK_SEARCH.class.getName()).log(Level.SEVERE, null, ex);
            }
            this.setVisible(false);
        }
    }                                          
    



    private void bookListValueChanged(javax.swing.event.ListSelectionEvent evt) {                                      

        clear();
        try {
            stat = con.createStatement();
            String str = (String) bookList.getSelectedValue();
            rset = stat.executeQuery("SELECT * FROM BOOK WHERE BOOK_TITLE = '"+ str +"'");

        if (rset.next())
        {
             displayBookCode.setText(rset.getString("BOOK_CODE"));
             displayBookTitle.setText(rset.getString("BOOK_TITLE"));
             displayBookAuthor.setText(rset.getString("BOOK_AUTHOR"));
             displayBookPublisher.setText(rset.getString("BOOK_PUBLISHER"));
             displayIsbn.setText(rset.getString("BOOK_ISBN"));
             String avai = rset.getString("BOOK_AVAILABLE");
             Bcode = rset.getString("BOOK_CODE");
            Charge1 = rset.getString("BOOK_CHARGE");
            Charge1.trim();
            Charge2 = rset.getString("BOOK_COST");
            Charge2.trim();

            // Double a= Double.parseDouble(Charge1) ;
            // Double b= Double.parseDouble(Charge2);
            // Double total= a+b;

            //totalCharge = total.toString();
          //  System.out.println("charge = "+totalCharge);

            if(avai.equals("YES"))
                {
                       availableComboBox.setSelectedIndex(0);
                }
                else
                {
                        availableComboBox.setSelectedIndex(1);
                }
            }


        } catch (SQLException ex) {
            Logger.getLogger(BOOK_SEARCH.class.getName()).log(Level.SEVERE, null, ex);
        }

     
    }                                     


    
    private void connect() throws SQLException
    {
        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
        System.out.println("Driver Loaded");
        con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","javaproject","javaproject");
        System.out.println("Database Connected");

    }
    /**
    * @param args the command line arguments
    */

     private void populateData() throws SQLException
    {
        String query = "SELECT BOOK_TITLE FROM BOOK";
        List = new DefaultListModel();
        stat = con.createStatement();
        rset = stat.executeQuery(query);
        while(rset.next())
        {
            List.addElement(rset.getString("BOOK_TITLE"));
        }
        bookList.setModel(List);
    }




    private void clear()
    {
        displayBookTitle.setText("");
        displayBookAuthor.setText("");
        displayBookPublisher.setText("");
        displayIsbn.setText("");

    }
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new BOOK_SEARCH().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(BOOK_SEARCH.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }


    // Variables declaration - do not modify                     
    private javax.swing.JButton RentButton;
    private javax.swing.JComboBox availableComboBox;
    private javax.swing.JButton backButton;
    private javax.swing.JLabel bookCodeLabel;
    private javax.swing.JLabel bookCodeLabel2;
    private javax.swing.JTextField bookCodeTextField;
    private javax.swing.JLabel bookISBNLabel;
    private javax.swing.JList bookList;
    private javax.swing.JLabel bookPublisherLabel;
    private javax.swing.JLabel bookTitleLabel;
    private javax.swing.JLabel bookauthorLabel;
    private javax.swing.JLabel bookavailableLabel;
    private javax.swing.JTextField displayBookAuthor;
    private javax.swing.JTextField displayBookCode;
    private javax.swing.JTextField displayBookPublisher;
    private javax.swing.JTextField displayBookTitle;
    private javax.swing.JTextField displayIsbn;
    private javax.swing.JTextField displaymemberCode;
    private javax.swing.JButton findButton;
    private javax.swing.JComboBox genreComboBox;
    private javax.swing.JLabel genreLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel memberCodeLabel;
    private javax.swing.JLabel searchBookLabel;
    // End of variables declaration                   

}
